home *** CD-ROM | disk | FTP | other *** search
/ Software USA 3 #11 / Software USA Volume 3.11.iso / mac / Education / Student Programmer / Computer Programming Lessons next >
Encoding:
Text File  |  1997-10-01  |  14.2 KB  |  350 lines  |  [TEXT/ttxt]

  1. Lesson One - HyperTalk!
  2.  
  3. Objective - Students will become familiar with basic computer programming terminology (HyperTalk, Scripting, Syntax).
  4.  
  5. BASIC
  6.     Computer Programming used to be very difficult.  Most computer programs used to look like random numbers and letters to the untrained eye.
  7.     Then came a programming language called “BASIC”.  BASIC was the first computer programming language that read like english.  Look at the following BASIC program:
  8.  
  9. 10  put 4 into a
  10. 20  if a = 4 then go to line 40
  11. 30  go to line 10
  12. 40  end
  13.  
  14. BASIC changed the way people programmed.  But it was not powerful enough to do many things, and it took hundreds of lines of code to make a worthwhile program.
  15.  
  16. HyperTalk
  17.     HyperCard, a program for that Mac that allows people to make their own computer programs, uses a computer programming language called HyperTalk.  HyperTalk is very powerful, yet it is oneof the easiest programming language to learn.  HyperTalk is the language we will be learning.
  18.  
  19. Scripts and Scripting
  20.     In HyperTalk, a script refers to a small program written in HyperTalk.  Just like an actress follows a script on stage, the computer will follow your script!  A script, which is a computer program, is a set of directions for the computer to follow.  If you give someone bad directions, they will get lost.  If you give a computer bad directions, it not do what you want it to.  In some cases, it will tell you that your directions are bad.
  21.  
  22. Syntax
  23.     Syntax is to HyperTalk what grammar is to the english language.  Syntax is the exact wording of scripts.  In HyperTalk, one wrong letter can totally change the function of a script, making it do somet•hing weird, or not do anything at all!
  24.  
  25.  
  26.  
  27. Question - Why did BASIC change the way people programmed?  
  28. Question - What is a “Script”? 
  29. Question - What will happen if you write a script with an error in it?  Question - What is “Syntax?”  
  30.  
  31.  
  32.  
  33. Lesson Two - The “Add” Command
  34.  
  35. Objective - Students will define a “card field”, understand how the “add” command works, and write scripts utilizing the “add” command.
  36.  
  37. Read the following script:
  38. on mouseUp
  39.      put 8 + 2 into card field “a”
  40. end mouseUp
  41.  
  42.     Type the above script into Student Programmer, and then run it.  Student Programmer will put the number 10 into card field “a”.  Card fields are areas on the screen where text can £be entered.  The first line of the script, “on mouseUp”, and the last line of the script, “end mouseUp”, are together called a “handler”.  They tell the computer to run the script when the user lets up on the mouse button.
  43.  
  44. Question - What is a card field?
  45. Question - How did the program know to put 10 into card field “a”?
  46. Question - Why did the program put 10 into card field “a” instead of “8 + 2”?
  47. Question - Why do you suppose the last line of this script is “end mouseUp”?
  48.  
  49. Extra Practice - Write your own script that adds two numbers together.  
  50. Extra Challenge - Write your own script that adds to numbers, and puts the answer in all 5 card fields (a through e)!
  51.  
  52. Lesson Three - The “Add” Command
  53.  
  54. Objective - Students will write scripts utilizing the “add” command.
  55.  
  56. 1.  Read the following script:
  57.  
  58. on mouseUp
  59.      put 10 into card field “a”
  60.      put 9 into card field “b”
  61.      put card field “a” + card field “b” into carˇd field “c”
  62. end mouseUp
  63.  
  64.     Type the above script into Student Programmer and run it.   Student Programmer will put 10 into field “a”, 9 into field “b”, and 19 into field “c”.
  65.  
  66. Question - Why does the program put 19 into card field “c”?
  67. Question - What would happen if you changed the 10 and 9 to 100 and 90?
  68. Extra Practice - Write a script that adds two numbers together and puts the answer into card field “e”.
  69. Extra Challenge - Write a script that adds four numbers together and puts the answer into card field “e”. Lesson Four - The “Subtract” Command
  70.  
  71. Objective - Students will write scripts utilizing the “add” command.
  72.  
  73. 1.  Type the following script into Student Programmer:
  74.  
  75. on mouseUp
  76.      put 9 - 2 into card field “a”
  77. end mouseUp
  78.  
  79. Student Programmer will put 7 into card field “a”.
  80.  
  81. 2.  Now, for something tricky!  Type the following script into Student Programmer.
  82.  
  83. on mouseUp
  84.      ask “Enter a number”
  85.      put it into card field “a”
  86.      ask “Enter another number”
  87.      put it into card field “b”
  88.      answer “Your two numbers add up to be” && card field “a” + card field “b”
  89. end mouseUp
  90.  
  91. You’ve just created a calculator!  Well, at least one that can add.  The “ask” command is very powerful, and you will learn more about it in later lessons.
  92.  
  93. Question - What does the “ask” command do?  (It brings up a dialog box that you can type in).
  94.  
  95. Extra Practice - Write a script that subtracts two numbers and puts the answer into card field “a”.
  96.  
  97. Extra Challenge - Write a script that puts a different number into card fields “c” and “d”, and then adds them together and puts the answer into card field “a”.  Use the above script as an example.
  98.  
  99.  
  100.  
  101. Lesson Five - The “Answer” Command.
  102.  
  103. Objective - Students will understand how the “answer” command works, and will write scripts utilizing the “answer” command.
  104.  
  105. The answer command brings up a “dialog” box, with response buttons.  The most common use for this is:
  106.  
  107.  
  108.  
  109.  
  110.  
  111. 1.  Try to duplicate the above dialog box by typing the following script:
  112.  
  113. on mouseUp
  114.      answer "Are you sure you want to quit?" with "Yes" or "No"
  115. end mouseUp
  116.  
  117. HyperTalk is a very powerful language!  In older computer languages, the above box might take 50 or more lines of code!  
  118.  
  119. 2.  Here’s another example of the “answer” command.  Try typing the following script into StuÅdent Programmer:
  120.  
  121. on mouseUp
  122.      answer “Are you cool?”
  123. end mouseUp
  124.  
  125. 3.  Now enter your own “answer” command.  See if you can also program 2 responses, such as “Yes” or “No”.   Notice that if you don’t program any choices for answers, HyperTalk gives you one choice: “OK”.
  126.  
  127. 4.  Now for the big challenge.  Try typing this script into Student Programmer:
  128.  
  129. on mouseUp
  130.      answer “Are you cool?  with “Yes” of “No”
  131.      if it is “Yes” then answer “No you’re not!”
  132.      if it is “No” then answer “Yes you are!”
  133. end mouseUp
  134.  
  135. Run the script.  You’ve just used a command and a statement - the “Answer” command and the “If/Then” statement.  “The If/Then” statement will be the topic of the next lesson.
  136.  
  137. Extra Assignment - Write a script that contains 4 different answer commands in a row! (Hint - Your script should be 6 lines long).
  138.  
  139. Lesson 6 - The If/Then statement
  140.  
  141. Objective - Students will underst¯and “if/then” logic, and will be able to write a script using an “if/then” statement.
  142.  
  143.  
  144.  
  145. 1.  Type in the following script:
  146.  
  147. on mouseUp
  148.      answer “Who is stronger, Superman or Spider Man?” with “Superman” or “Spider Man”
  149.      if it is “Superman”  then answer  “You are right!”
  150.      if it is “Spider Man”  then answer  “You are wrong!”
  151. end mouseUp
  152.  
  153. The If/Then statement is conditional - If the user clicked on the “Superman” button then the program will answer “You are right!”  In computer programming, we have a way of showing how this works:     
  154. If the
  155. user clicks on
  156.  
  157. Superman                       Spider Man
  158.  
  159.  
  160.  
  161.  
  162.  
  163.          Then answer                                    Then answer
  164.          “You are right!”                                    “You are wrong!”
  165.  
  166.  
  167.  
  168.  
  169. If the user clicks on one button, then one event will happen.  If they click on the other button, then a different event will happen.  That’s why this is called an “If/Then” statement!
  170.  
  171. 2.  Now let’s try something more advanced.  Type the script below into Student Programmer.  Instead of the blank lines, write your own answers.  Use the script from #1 (above) as an example.
  172.  
  173. on mouseUp
  174.      answer “Do you like computers?” with “Yes” or “No”
  175.      if it is “Yes” then answer “____________________________________”
  176.      if it is “No” then answer “ _____________________________________”
  177. end mouseUp
  178.  
  179. Did you get your script to run?  Good for you!  If not, keep trying.
  180.  
  181.  
  182. Lesson 7 - The If/Then Statement
  183.  
  184. Objective - Students will understand “if/then” logic, and will be able to write a script using an “if/then” statement.
  185.  
  186. 1.  Type the following script into Student Programmer:
  187.  
  188. on mouseUp
  189.      answer “Are you cool?” with “Yes” or “No”
  190.      if it is “Yes” then
  191.           answer “Are you sure?” with “Yes” or “No”
  192.                if it is “Yes” then answer “Sure you are!”
  193.                if it is “No” then answer “You changed your mind.”
  194.      end if
  195.      if it is “No” then
  196.           answer “Don’t be so hard on yourself!”
  197.           answer “You’re cool.  Honest you are.”
  198.      end if
  199. end mouseUp
  200.  
  201. If you can get the above script to work, you’re doing well!  Notice that the If/Then statements above start with “if it is “Yes” then... and end with “end if”.  You can put an almost unlimited number of directions between the “if...” line and the “end if” line.  
  202.  
  203. In problem number 3 (above), you can begin to see how scripts can quickly become complicated.  It’s important to script slowly so that you understand what happening with each line.
  204.  
  205. Question - What does an “If/Then” statement do?
  206. Question - How many directions can be between an “if” line and the “end if” line in a script?
  207.  
  208. Extra Practice - Make a script that contains an If/Then statement.  Use the script above as an example.
  209. Extra Credit - Make a script that contains more than one If/Then statement.  Use the script above as an example.
  210.  
  211. Lesson 8 - The Repeat Loop
  212.  
  213. Objective - Students will understand repeat loops.
  214.  
  215. 1.  The repeat loop is one of the most powerful programming tools.  Type the following script into Student Programmer:
  216.  
  217. on mouseUp
  218.      repeat for 5 times
  219.           answer “This is a test.”
  220.      end repeat
  221. end mouseUp
  222.  
  223. When you run this script, you will be forced to click the “OK” button 5 times.  
  224.  
  225. 2.  Type the following script into Student Programmer:
  226.  
  227. on mouseUp
  228.      put 2 into card field “a”
  229.      repeat for 20 times
  230.           add 2 to card field “a”
  231.      end repeat
  232. end mouseUp
  233.  
  234. What did it do?  If you programmed it correctly, it put 2 into card field “a” and then added 2 to that number 20 times!
  235.  
  236. DANGER! - The repeat loop is the beginning programmer’s worst nightmare when used incorrectly!
  237.  
  238. DO NOT TYPE THE FOLLOWING SCRIPT INTO STUDENT PROGRAMMER!
  239.  
  240. on mouseUp
  241.      repeat
  242.           answer “How are you?” with “Fine.”
  243.      end repeat
  244. end mouseUp
  245.  
  246. What would happen if you ran that script?  Without telling the computer how many times to repeat, the computer would repeat forever.  
  247.  
  248. IF YOU ACCIDENTALLY WRITE A BAD REPEAT LOOP, DO THIS:  Hold the “Command Key” down and press the “.” (period) key.  This will automatically stop your script from running!
  249.  
  250. Question - What does the repeat loop do?
  251. Question - Why do we have to tell the repeat loop how many times to repeat?
  252. Lesson 9 - The Repeat Loop + the If/Then Statement
  253.  
  254. Objective - Students will combine a repeat loop with an “if/then” statement.
  255.  
  256. Computer programming is all about combining commands to do different things!  Here’s a cool script you can try:
  257.  
  258. on mouseUp
  259.      repeat
  260.           answer “Are you happy?” with “Yes” or “No”
  261.           if it is “Yes” then exit repeat
  262.           if it is “No” then 
  263.                answer “You need to be happy!”
  264.                answer “Life is too short to be sad!”
  265.                answer “Try to be happy all of the time...”
  266.                answer “and you will be so glad!”
  267.           end if
  268.      end repeat
  269.      answer “I’m glad you’re happy!”
  270. end mouseUp
  271.  
  272. What happens when you run this script?  The computer will not let you out of the repeat loop until you click the “Yes” button.  When you do that, the “exit repeat” command is given.  This is another way to get out of a repeat loop!
  273.  
  274. Question - What does the above script do when the user clicks the “Yes” button?
  275.  
  276. Extra Practice - Write a script containing a repeat loop AND an “if/then” statement.
  277.  
  278. Extra Credit - Write a script containing a repeat loop and 2 “if/then”statements!
  279.  
  280.  
  281. Lesson 10 - Advanced Project
  282.  
  283. Objective - Students will enter a script and then modify it to suit their needs.
  284.  
  285. Below is a script.  Enter it into Student Programmer, and then (after you’ve gotten it to run) feel free to modify it!
  286.  
  287. IMPORTANT!  When you come to the end of the line, keep typing.  Even though the line will wrap, it will still be considered part of the same line.  
  288.  
  289. on mouseUp
  290.     answer “Wacky Psychiatrist by R. MacLemale”
  291.     ask “Hello.  What is your name?”
  292.     put it into newName
  293.     ask “Pleased to meet you,” && newName & “.  What seems to be the problem?”
  294.     put it into problem
  295.     ask “So when you say” && problem && “, aren’t you really saying you are silly?”
  296.     ask “Why do you say” && it & “?”
  297.     answer “There’s no reason to talk that way!”
  298.     answer “Is this problem a little problem or a big problem?”  with “Little” or “Big”
  299.     if it is “Little” then
  300.         ask “Why are you bugging me with such a little problem?”
  301.         answer “Well you need to get a grip!  Take two aspirin and swallow a car.”
  302.     else
  303.         answer “Ooooo, a BIG problem.  You think you’ve got a big problem?” with “Yes”
  304.         answer “So” && quote & problem & quote && “is a big problem?” with “Yes”
  305.         answer “I guess it is.  Drink a glass of water and read a book.”
  306.     end if
  307.     answer “Would you like to argue?” with “Yes” or “No”
  308.     if it is “Yes” then
  309.         repeat
  310.             answer “Are you a loser?” with “Yes” or “No”
  311.             if it is “No” then
  312.                 answer “Yes you are!” with “No way!”
  313.             else
  314.                 answer “No you’re not!”
  315.                 exit repeat
  316.             end if
  317.         end repeat
  318.     end if
  319.     answer “That will be three thousand dollars, please.” with “No”
  320. end mouseUp
  321.  
  322. Lesson 11 - Advanced Project
  323.  
  324. Objective - Students will enter a script and then modify it to suit their needs.
  325.  
  326. Below is a script.  Enter it into Student Programmer, and then (after you’ve gotten it to run) feel free to modify it!d
  327.  
  328. IMPORTANT!  When you come to the end of the line, keep typing.  Even though the line will wrap, it will still be considered part of the same line.  
  329.  
  330. on mouseUp
  331.    answer “Magic 8 Ball Simulation by R. MacLemale.”
  332.    ask “What is your name?”
  333.    put it into name
  334.    repeat
  335.     ask “Please type a yes or no question,” && name & “:”
  336.     set cursor to watch
  337.     wait 3 seconds
  338.     put random(6) into thisAnswer
  339.     if thisAnswer = 1 then answer “My sources say no.”
  340.     if thisAnswer = 2 then answer “Count on it!”
  341.     if thisAnswer = 3 then answer “No.”
  342.     if thisAnswer = 4 then answer “Yes.”
  343.     if thisAnswer = 5 then answer “Ask again later.”
  344.     if thisAnswer = 6 then answer “You bet!”
  345.     answer “That was fun.  Would you like to ask another question?” with “Yes” or “No”
  346.     if it is “No” then exit to HyperCard
  347.    end repeat
  348. end mouseUp
  349.  
  350. Can you modify this script to include up to 8 responses?